package it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.datatable.DataTableTag;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;

public class ObjectUtils
{
	private final static Logger logger = LoggerFactory.getLogger(ObjectUtils.class);

	public static Object getBeanProperty(String paramString, Object paramObject) throws SecurityException,
			NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
	{
		String str = "get" + Character.toUpperCase(paramString.charAt(0)) + paramString.substring(1);
		Method localMethod = paramObject.getClass().getMethod(str, new Class[0]);
		return localMethod.invoke(paramObject, new Object[0]);
	}

	public static Object getProperty(String paramString, Object paramObject) throws Exception
	{
		String[] arrayOfString = paramString.split("\\.");
		if(arrayOfString.length > 1)
		{
			paramObject = getProperty(paramString.substring(0, paramString.lastIndexOf(".")), paramObject);
			paramString = arrayOfString[(arrayOfString.length - 1)];
		}
		try
		{
			if(paramObject instanceof Map)
			{
				Map localMap = (Map) paramObject;
				return localMap.get(paramString);
			}
			return getBeanProperty(paramString, paramObject);
		}
		catch (InvocationTargetException localInvocationTargetException)
		{
			throw new Exception(localInvocationTargetException);
		}
		catch (Exception localException)
		{
			logger.debug(
					"Exception occurred getting property " + paramString + " from a bean of class "
							+ paramObject.getClass(), localException);
		}
		return null;
	}

	public static String serializeObject(Object paramObject) throws IOException
	{
		ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(localByteArrayOutputStream);
		localObjectOutputStream.writeObject(paramObject);
		byte[] arrayOfByte = Base64.encodeBase64(localByteArrayOutputStream.toByteArray());
		try
		{
			localObjectOutputStream.close();
		}
		catch (IOException localIOException)
		{
			logger.warn("Impossible to close ObjectOutputStream after serialization.");
		}
		return new String(arrayOfByte);
	}

	public static Object deserializeObject(String paramString) throws IOException, ClassNotFoundException
	{
		byte[] arrayOfByte = Base64.decodeBase64(paramString.getBytes());
		ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(arrayOfByte);
		ObjectInputStream localObjectInputStream = new ObjectInputStream(localByteArrayInputStream);
		try
		{
			localObjectInputStream.close();
		}
		catch (IOException localIOException)
		{
			logger.warn("Impossible to close ObjectInputStream after deserialization.");
		}
		return localObjectInputStream.readObject();
	}
}
